package com.shanghaionstar.carmanage.service;

import android.content.Context;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.onstar.cn.api.GprsPackageService;
import com.onstar.cn.common.exception.OnStarException;
import com.onstar.cn.common.util.CommonUtil;
import com.onstar.cn.common.util.Constants;
import com.onstar.cn.framework.annotation.http.HeadsValue;
import com.onstar.cn.framework.annotation.http.Request;
import com.onstar.cn.framework.mvp.base.core.BaseService;
import com.onstar.cn.framework.network.HttpInjectUtils;
import com.onstar.cn.framework.network.entity.RequestEntity;
import com.onstar.cn.framework.network.enumtype.HttpAccept;
import com.onstar.cn.framework.network.enumtype.HttpMethod;
import com.onstar.cn.framework.network.enumtype.Language;
import com.onstar.cn.framework.utils.GsonHelper;
import com.onstar.cn.mag.enumtype.PackageType;
import com.onstar.cn.more.entity.PackageInfo;
import com.onstar.cn.more.response.DataPackageRule;
import com.shanghaionstar.utils.ApplicationUtil;
import com.shanghaionstar.utils.BusinessUtil;
import com.shanghaionstar.utils.FinalConstants;
import com.shanghaionstar.utils.NetConnectUtil;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

import rx.Observable;
import rx.functions.Func0;

/**
 * Created by Yonglang Hong on 2016/7/26.
 */
public class FlowPackageService extends BaseService{

    private Context context;

    public FlowPackageService(Context context) {
        this.context = context;
    }

    @Override
    public RequestEntity getRequestEntity() {
        return null;
    }

    @Override
    public Object jsonToBean(String json) {
        return null;
    }

    public Observable<List<PackageInfo>> getFlowPackage(final PackageType packageTypes) {
        return Observable.defer(new Func0<Observable<List<PackageInfo>>>() {
            @Override
            public Observable<List<PackageInfo>> call() {
                try {
                    return Observable.just(doGetFlowPackage(packageTypes));
                } catch (OnStarException e) {
                    return Observable.error(e);
                }
            }
        });
    }

    public Observable<DataPackageRule> getflowPackageRule() {
        return Observable.defer(new Func0<Observable<DataPackageRule>>() {
            @Override
            public Observable<DataPackageRule> call() {
                try {
                    return Observable.just(doGetflowPackageRule());
                } catch (OnStarException e) {
                    return Observable.error(e);
                }
            }
        });
    }

    @Request(heads = @HeadsValue(accept = HttpAccept.JSON, language = Language.AUTO), method = HttpMethod.GET)
    public List<PackageInfo> doGetFlowPackage(final PackageType packageTypes) throws OnStarException {
        String subscriberID = ApplicationUtil.getInstance().getSubscriberID();
        String accountID = ApplicationUtil.getInstance().getAccountId();
        GprsPackageService service = new GprsPackageService(Constants.CURRENT_SERVER_URL,context);
        RequestEntity requestEntity = NetConnectUtil.getAuthRequestEntity(context);

        service.getPacakagesByVinService(requestEntity, subscriberID, accountID, ApplicationUtil.getInstance().getVin(),packageTypes.value());
        requestEntity =  HttpInjectUtils.injectHttp(getClass(), "doGetFlowPackage", requestEntity,PackageType.class);
        String response = NetConnectUtil.getResponse(getDatas(requestEntity));

        return new Gson().fromJson(response,new TypeToken<ArrayList<PackageInfo>>(){}.getType());
    }

    @Request(heads = @HeadsValue(accept = HttpAccept.JSON, language = Language.AUTO), method = HttpMethod.GET)
    public DataPackageRule doGetflowPackageRule() throws OnStarException{
        String subscriberID = ApplicationUtil.getInstance().getSubscriberID();
        String accountID = ApplicationUtil.getInstance().getAccountId();
        String vin = null;
        vin = ApplicationUtil.getInstance().getVin();
        GprsPackageService service = new GprsPackageService(Constants.CURRENT_SERVER_URL,context);
        RequestEntity requestEntity = NetConnectUtil.getAuthRequestEntity(context);
        service.getData(requestEntity, subscriberID, accountID, vin);
        LinkedHashMap<String, Object> coHashMap = new LinkedHashMap<>();
        coHashMap.put(FinalConstants.HTTP_AUTHORIZATION, ApplicationUtil.getInstance().getToken());
        requestEntity.setCustomHeads(coHashMap);
        requestEntity =  HttpInjectUtils.injectHttp(getClass(), "doGetflowPackageRule", requestEntity);
        String response = NetConnectUtil.getResponse(getDatas(requestEntity));
        return GsonHelper.fromJson(response,DataPackageRule.class);
    }

}
